/*
* Copyright (c) 2017 Zhangyujie (Tocy)

* This file is part of step-into-sdl2 project.
*/

#include "digit.h"
#include <iostream>
using namespace std;


void printf_rect(const SDL_Rect & rect)
{
	cout << "rect info\n";
	cout << "x:" << rect.x << " y: " << rect.y << endl;
	cout << "w:" << rect.w << " h: " << rect.h << endl << endl;
}

SegmentDigit::SegmentDigit()
: m_render(NULL), m_margin(SEGMARGIN)
, m_mask(0x7F)
{
	cout << sizeof(m_seg_rect) << endl;
	SDL_memset(m_seg_rect, 0, sizeof(m_seg_rect));
}

SegmentDigit::~SegmentDigit()
{
	uninit();
}

void SegmentDigit::init(SDL_Renderer * render, const SDL_Rect &rect)
{
	m_render = render;
	cout << "enter init to calc\n";
	calculate_pos(rect);
}
void SegmentDigit::uninit()
{
	
}

void SegmentDigit::set_draw_mask(unsigned char mask)
{
	cout << "draw mask changing " << (int)mask << endl;
	switch(mask)
	{
	case 0:
		m_mask = 0x3F;
		break;
	case 1:
		m_mask = 0x06;
		break;
	case 2:
		m_mask = 0x5B;
		break;
	case 3:
		m_mask = 0x4F;
		break;
	case 4:
		m_mask = 0x66;
		break;
	case 5:
		m_mask = 0x6D;
		break;
	case 6:
		m_mask = 0x7D;
		break;
	case 7:
		m_mask = 0x07;
		break;
	case 8:
		m_mask = 0x7F;
		break;
	case 9:
		m_mask = 0x6F;
		break;
	default:
		m_mask = 0x79;
		break;
	}
}

void SegmentDigit::draw_rect(const SDL_Rect & rc)
{
#define POINTS_COUNT 5
	SDL_Point points[POINTS_COUNT] = {
		{rc.x, rc.y},
		{rc.x + rc.w, rc.y},
		{rc.x + rc.w, rc.y + rc.h},
		{rc.x, rc.y + rc.h},
		{rc.x, rc.y}
	};
	SDL_SetRenderDrawColor(m_render, 0, 0, 0x10, 0);
	SDL_RenderDrawLines(m_render, points, POINTS_COUNT);
}
void SegmentDigit::draw()
{
	//SDL_SetRenderDrawBlendMode(m_render, SDL_BLENDMODE_BLEND);
	unsigned char flag = 1;
	for (int i = 0; i < SEGMENTS; ++i)
	{
		if (m_mask&flag)
		{
			SDL_SetRenderDrawColor(m_render, 0xFF, 0, 0, 0);
			SDL_RenderFillRect(m_render, m_seg_rect+i);
		}
		else
		{
			//SDL_RenderDrawRect(m_render, m_seg_rect+i);
			//draw_rect(m_seg_rect[i]);
		}
		flag <<= 1;
	}
	//SDL_RenderFillRects(m_render, m_seg_rect, SEGMENTS);
}

void inflate_rect(SDL_Rect &rc, int dx, int dy)
{
	rc.x -= dx;
	rc.y -= dy;
	rc.w += (2*dx);
	rc.h += (2*dy);
}
void SegmentDigit::calculate_pos(const SDL_Rect & rc)
{		
	int middle = (rc.y + rc.h/2);

	SDL_Rect outer_rc = rc;
	inflate_rect(outer_rc, -m_margin, -m_margin);
	SDL_Rect inner_rc = outer_rc;
	inflate_rect(inner_rc, -m_margin, -m_margin);

	//printf_rect(outer_rc);
	//printf_rect(inner_rc);

	// U
	m_seg_rect[U].x = inner_rc.x; m_seg_rect[U].y = outer_rc.y;
	m_seg_rect[U].w = inner_rc.w; m_seg_rect[U].h = m_margin;
	//printf_rect(m_seg_rect[U]);

	// RU
	m_seg_rect[RU].x = inner_rc.x + inner_rc.w;
	m_seg_rect[RU].y = inner_rc.y;
	m_seg_rect[RU].w = m_margin;
	m_seg_rect[RU].h = (inner_rc.h-m_margin)/2;
	//printf_rect(m_seg_rect[RU]);


	// RD
	m_seg_rect[RD].x = inner_rc.x + inner_rc.w;
	m_seg_rect[RD].y = middle + m_margin/2;
	m_seg_rect[RD].w = m_margin;
	m_seg_rect[RD].h = inner_rc.h/2;
	//printf_rect(m_seg_rect[RD]);

	// D
	m_seg_rect[D].x = inner_rc.x; m_seg_rect[D].y = inner_rc.y + inner_rc.h;
	m_seg_rect[D].w = inner_rc.w; m_seg_rect[D].h = m_margin;
	//printf_rect(m_seg_rect[D]);
 
	// LD
	m_seg_rect[LD].x = outer_rc.x;
	m_seg_rect[LD].y = middle + m_margin/2;
	m_seg_rect[LD].w = m_margin;
	m_seg_rect[LD].h = inner_rc.h/2;
	//printf_rect(m_seg_rect[LD]);

	// LU
	m_seg_rect[LU].x = outer_rc.x;
	m_seg_rect[LU].y = inner_rc.y;
	m_seg_rect[LU].w = m_margin;
	m_seg_rect[LU].h = (inner_rc.h-m_margin)/2;
	//printf_rect(m_seg_rect[LU]);

	// M
	m_seg_rect[M].x = inner_rc.x; m_seg_rect[M].y = middle - m_margin/2;
	m_seg_rect[M].w = inner_rc.w; m_seg_rect[M].h = m_margin;
	//printf_rect(m_seg_rect[M]);
}